/**
 * 地址填充
 *
 * @project sky-take-out2
 * @author MECHREVO
 * @date 2023/12/10 21:13:04
 * @version 1.0
 */
package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 添加地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result<?> addAddress(@RequestBody AddressBook addressBook) {
        addressService.addAddress(addressBook);
        return Result.success();
    }

    /**
     * 查询地址
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        List<AddressBook> addressBooks = addressService.list();
        return Result.success(addressBooks);
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<?> delete(Long id) {
        addressService.delete(id);
        return Result.success();
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id) {
        AddressBook addressBook = addressService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 更新地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook) {
        addressService.update(addressBook);
        return Result.success();
    }

    /**
     * 设设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        addressService.setDefault(addressBook);
        return Result.success();
    }

    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        AddressBook addressBook = addressService.getDefault();
        return Result.success(addressBook);
    }
}
